export default {
	// tarbar的红点提示
	prompt(state) {
		// 对话页的通知
		let indexSum = state.notifySum + state.dialogueUnRead + ''
		if (indexSum > 0) {
			uni.setTabBarBadge({
				index: 0,
				text: indexSum
			})
		} else {
			uni.removeTabBarBadge({
				index: 0
			})
		}
		
		// 好友申请
		if (state.acquire > 0) {
			uni.setTabBarBadge({
				index: 1,
				text: state.acquire + ''
			});
		} else {
			uni.removeTabBarBadge({
				index: 1
			});
		}

		//发现页的通知(新访客，未读留言，新动态，新评论)
		if (state.newVisitor > 0 || state.messCount > 0 || state.dyNotify.length > 0 || state.count > 0) {
			uni.showTabBarRedDot({
				index: 2
			})
		} else {
			uni.hideTabBarRedDot({
				index: 2
			})
		}
	},
	// 修改聊天类型
	changeChatType(state, type) {
		state.chatType = type
	},
	// 修改聊天id
	changeChatId(state, id) {
		state.chatId = id
	},
	// 更新聊天信息（本地添加，减少请求）
	updateChatMsg(state, data) {
		//console.log("更新")
		//console.log(data.msg_list)
		if(data.msg_list != null){
				data.msg_list.push(data.msg.messageContent)
		}
		else{
			var temp = {
				chatId:data.msg.fromUser,
				msg_list:[]
			}
			temp.msg_list.push(data.msg.messageContent)
			state.chatHistory.push(temp)
			console.log(state.chatHistory)
		}
	
	},
	// 访问者的页码
	changeVisiPage(state, page) {
		state.visi_page = page
	},
	// 未读访问者信息
	changeVisitors(state, data) {
		state.visitors = data
	},
	// 修改评论通知（未读的条数）
	changeMessNotify(state, data) {
		let {
			user,
			date
		} = data
		let index = state.messNotify.findIndex(item => {
			return item.user == user && new Date(item.date).getTime() == new Date(date).getTime()
		})
		state.messNotify.splice(index, 1)
	},
	// 修改加载动画的标识
	changeIsLoading(state) {
		state.isLoading = !state.isLoading
	},
	// 修改聊天的用户名（群名）
	changeChatName(state, name) {
		state.name = name
	},
	// 聊天记录
	changeChatHistory(state, history) {
		state.chatHistory = history
	},
	
	// 群详情
	changeGroupInfo(state,obj){
		state.groupInfo = obj.group;
		this.commit("changeChatName",obj.group.name)
		state.g_nickName = obj.nickName;
	},
	// 动态通知表
	changeDyNotify(state, array) {
		state.dyNotify = array
		this.commit("prompt")
	},
	// 评论通知表
	changeComNotify(state, array) {
		state.comNotify = array
		state.comNotify.map(item => {
			if (!item.unRead) {
				state.count++
			}
		})
		this.commit("prompt")
	},

	// 好友请求的数量
	changeAcquire(state) {
		state.acquire +=1;
		// 好友申请
		if (state.acquire > 0) {
			console.log(state.acquire)
			console.log("显示")
			uni.setTabBarBadge({
				index: 1,
				text: state.acquire + ''
			});
		} else {
			console.log(state.acquire)
			console.log("隐藏")
			uni.removeTabBarBadge({
				index: 1
			});
		}
		//this.commit("prompt")
	},
	removeAcquire(state){
		state.acquire= 0;
	},
	remove_TabBarBadge(state){
		if (state.acquire > 0) {
			console.log(state.acquire)
			console.log("显示")
			uni.setTabBarBadge({
				index: 1,
				text: state.acquire + ''
			});
		} else {
			console.log(state.acquire)
			console.log("隐藏")
			uni.removeTabBarBadge({
				index: 1
			});
		}
	},
	// 通知的数量
	changeNotifySum(state, number) {
		state.notifySum = number
		this.commit("prompt")
	},
	// 对话表里未读的信息数量
	changeDialogueUnRead(state, number) {
		state.dialogueUnRead = number
		this.commit("prompt")
	},
	// 新访客数量
	changeNewVisitorSum(state, number) {
		state.newVisitor = number
		this.commit("prompt")
	},
	// 未读留言
	changeMessageSum(state, number) {
		state.messCount = number
		this.commit("prompt")
	},
	
	changeUser(state,user){
		state.user = user
	},
	
	changNewMsgCount(state){
		state.newMsgCount = 0;
		for(var i=0;i<state.dialogueList.length;i++){
			state.newMsgCount +=state.dialogueList[i].tip
		}
		if(state.newMsgCount!=0){
			uni.setTabBarBadge({
				index: 0,
				text: state.newMsgCount +''
			})
		}
		else{
			uni.removeTabBarBadge({
				index:0
			})
		}
	},
	changeDialogueList(state,newDialogueList){
		state.dialogueList = newDialogueList
	},
	// 1.不在聊天页面：来一条未读加一，改变显示内容
	// 2.在聊天页面
			//	2.1  聊天的人发来消息，只改变显示内容
			//  2.2  其他人发消息，同1
	changDialogueInfo(state,msg){
		console.log("测试一下1",msg)
		for(var i=0;i<state.dialogueList.length;i++){
				//公共聊天室
				//console.log("测试一下2",msg.fromUser)
				if(msg.chatType == 'public' && msg.toUser == state.dialogueList[i].username){
					//console.log("测试一下3",msg.fromUser)
					if(state.chatId == 'All'){
						//console.log("测试一下",msg.fromUser)
						if(msg.fromUser!= state.user.username)
						{
							if(msg.messageContent.types == 0){
								state.dialogueList[i].message =msg.messageContent.nickname +"："+ msg.messageContent.message
							}
							else if(msg.messageContent.types == 1)
							{
								state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[图片]";
							}
							else if(msg.messageContent.types == 2)
							{
								state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[语音]";
							}
							else if(msg.messageContent.types == 3)
							{
								state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[位置]";
							}
							
							state.dialogueList[i].time = msg.sendTime
						}
						else{
						//	console.log("测试一下4",msg.fromUser)
						if(msg.messageContent.types == 0){
							state.dialogueList[i].message = msg.messageContent.message
						}
						else if(msg.messageContent.types == 1)
						{
							state.dialogueList[i].message ="[图片]";
						}
						else if(msg.messageContent.types == 2)
						{
							state.dialogueList[i].message ="[语音]";
						}
						else if(msg.messageContent.types == 3)
						{
							state.dialogueList[i].message ="[位置]";
						}
							state.dialogueList[i].time = msg.sendTime
						}
						return
					}
					state.dialogueList[i].tip++;
					if(msg.messageContent.types == 0){
						state.dialogueList[i].message =msg.messageContent.nickname +"："+ msg.messageContent.message
					}
					else if(msg.messageContent.types == 1)
					{
						state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[图片]";
					}
					else if(msg.messageContent.types == 2)
					{
						state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[语音]";
					}
					else if(msg.messageContent.types == 3)
					{
						state.dialogueList[i].message =msg.messageContent.nickname +"："+ "[位置]";
					}
					// state.dialogueList[i].message =msg.messageContent.nickname +"："+ msg.messageContent.message
					state.dialogueList[i].time = msg.sendTime
					var temp = state.dialogueList[i];
					state.dialogueList.splice(i,1);
					state.dialogueList.unshift(temp)
					return
				}
				else{
					//私聊
					
					if((state.dialogueList[i].username == msg.fromUser  || state.dialogueList[i].username == msg.toUser)&&(msg.fromUser == state.chatId|| msg.toUser == state.chatId)){
							if(msg.messageContent.types == 0){
								state.dialogueList[i].message = msg.messageContent.message
							}
							else if(msg.messageContent.types == 1)
							{
								state.dialogueList[i].message ="[图片]";
							}
							else if(msg.messageContent.types == 2)
							{
								state.dialogueList[i].message ="[语音]";
							}
							else if(msg.messageContent.types == 3)
							{
								state.dialogueList[i].message ="[位置]";
							}
							//state.dialogueList[i].message = msg.messageContent.message
							state.dialogueList[i].time = msg.sendTime
							return
						}
					else if(msg.fromUser ==state.dialogueList[i].username && msg.toUser == state.user.username && msg.fromUser != state.chatId){
							state.dialogueList[i].tip++;
							if(msg.messageContent.types == 0){
								state.dialogueList[i].message = msg.messageContent.message
							}
							else if(msg.messageContent.types == 1)
							{
								state.dialogueList[i].message ="[图片]";
							}
							else if(msg.messageContent.types == 2)
							{
								state.dialogueList[i].message ="[语音]";
							}
							else if(msg.messageContent.types == 3)
							{
								state.dialogueList[i].message ="[位置]";
							}
							//state.dialogueList[i].message = msg.messageContent.message
							state.dialogueList[i].profilePhoto = msg.messageContent.profilePhoto
							state.dialogueList[i].time = msg.sendTime
							var temp = state.dialogueList[i];
							state.dialogueList.splice(i,1);
							state.dialogueList.unshift(temp)
							return
						}
				}
				
				}
				if(msg.fromUser != state.user.username){
					//console.log("不再好友列表的发消息过来5")
						for(var i = 0;i<state.friend_list.length;i++){
							if(state.friend_list[i].username == msg.fromUser){
								//console.log("不再好友列表的发消息过来4")
								var newDialogue = {
									id: 0,
									message: msg.messageContent.message,
									profilePhoto:state.friend_list[i].profilePhoto,
									time: msg.sendTime,
									tip: 1,
									username: msg.fromUser,
									nickname:state.friend_list[i].nickname
								}
								if(msg.messageContent.types == 0){
									newDialogue.message = msg.messageContent.message
								}
								else if(msg.messageContent.types == 1)
								{
									newDialogue.message = "[图片]";
								}
								else if(msg.messageContent.types == 2)
								{
									newDialogue.message =  "[语音]";
								}
								else if(msg.messageContent.types == 3)
								{
									newDialogue.message = "[位置]";
								}
								state.dialogueList.unshift(newDialogue)
								return
							}
						
						}
						//不再好友列表的发消息过来
						console.log("不再好友列表的发消息过来")
						var newDialogue = {
							id: 0,
							message: msg.messageContent.message,
							profilePhoto:msg.messageContent.profilePhoto,
							time: msg.sendTime,
							tip: 0,
							username: msg.fromUser,
							nickname:msg.messageContent.nickname
						}
						state.dialogueList.unshift(newDialogue)
						state.dialogueList[0].tip++;
						return
					}
					else{
						//自己去找没有聊过天的人  需要再聊天列表展示
						//需要通过接口查找信息
						console.log("不再好友列表的发消息过来3")
						for(var i = 0;i<state.friend_list.length;i++){
							
							if(state.friend_list[i].username == msg.toUser){
								console.log("不再好友列表的发消息过来2")
								var newDialogue = {
									id: 0,
									message: msg.messageContent.message,
									profilePhoto:state.friend_list[i].profilePhoto,
									time: msg.sendTime,
									tip: 0,
									username: msg.toUser,
									nickname:state.friend_list[i].nickname
								}
								if(msg.messageContent.types == 0){
									newDialogue.message = msg.messageContent.message
								}
								else if(msg.messageContent.types == 1)
								{
									newDialogue.message = "[图片]";
								}
								else if(msg.messageContent.types == 2)
								{
									newDialogue.message =  "[语音]";
								}
								else if(msg.messageContent.types == 3)
								{
									newDialogue.message = "[位置]";
								}
								state.dialogueList.unshift(newDialogue)
								return
							}
						}
						//不再好友列表的发消息过来
						console.log("不再好友列表的发消息过来")
						var newDialogue = {
							id: 0,
							message: msg.messageContent.message,
							profilePhoto:msg.messageContent.profilePhoto,
							time: msg.sendTime,
							tip: 0,
							username: msg.toUser,
							nickname:msg.messageContent.nickname
						}
						state.dialogueList.unshift(newDialogue)
						return
						}

			}
			
			
		
}
